﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CanDoo;
using CanDoo.Core;
using CanDoo.Core.Data;
using CanDoo.Data;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.DTOs;

namespace CanDoo.Oil.Core.Services
{
    /// <summary>
    /// 业务实现——销售单
    /// </summary>
    public class OutOrderService : CanDoo.Data.Entity.Services.ServiceBase<OutOrder>, IOutOrderContract
    {
        public IOutStoreDetailContract OutStoreDetailContract { get; set; }
        public IOutSellBackDetailContract OutSellBackDetailContract { get; set; }
        public OutOrderService()
        {
        
        }

        public string GetNewCode()
        {
            var startStr = "XS";
            var dateStr = DateTime.Now.ToString("yyMMdd");
            var codePrefix = startStr + dateStr;

            //生成新单据号
            var newCode = "";
            var entity = Entities.Where(p => p.Code.StartsWith(codePrefix)).OrderByDescending(p => p.Code).FirstOrDefault();
            if (entity == null)
                newCode = codePrefix + "0001";
            else
                newCode = startStr + (int.Parse(entity.Code.Substring(startStr.Length)) + 1);
            return newCode;
        }

        public OperationResult UpdateNum(long entityId)
        {
            var result = OperationResult.Success;
            using (var scope = DbScopeFactory.Create())
            {
                var entity = GetById(entityId);
                bool IsAllGet = true;
                bool IsNoGet = true;

                var thisTotalNum = 0.0;
                var thisTotalMoney = 0.0;

                foreach (var m in entity.OutOrderDetails)
                {
                    var InNumSum = OutStoreDetailContract.Entities.Where(p => p.OutStore.PassState == PassState.Pass && p.LinkEntityId == m.Id).Select(p => p.Num).DefaultIfEmpty(0.00).Sum();
                    var InMoneySum = OutStoreDetailContract.Entities.Where(p => p.OutStore.PassState == PassState.Pass && p.LinkEntityId == m.Id).Select(p => p.Money).DefaultIfEmpty(0.00).Sum();

                    var OutNumSum = OutSellBackDetailContract.Entities.Where(p => p.OutSellBack.PassState == PassState.Pass && p.LinkEntity.LinkEntityId == m.Id).Select(p => p.Num).DefaultIfEmpty(0.00).Sum();
                    var OutMoneySum = OutSellBackDetailContract.Entities.Where(p => p.OutSellBack.PassState == PassState.Pass && p.LinkEntity.LinkEntityId == m.Id).Select(p => p.Money).DefaultIfEmpty(0.00).Sum();

                    var inStoreNum = InNumSum - OutNumSum;
                    var inStoreMoney = InMoneySum - OutMoneySum;
                    if (inStoreNum < 0)
                        inStoreNum = 0;

                    if (inStoreMoney < 0)
                        inStoreMoney = 0;

                    if (inStoreNum > 0)
                        IsNoGet = false;
                    if (inStoreNum < m.Num)
                        IsAllGet = false;

                    m.AlreadyNum = inStoreNum;
                    m.AlreadyMoney = inStoreMoney;

                    thisTotalNum += inStoreNum;
                    thisTotalMoney += inStoreMoney;
                }

                entity.AlreadyMoney = thisTotalMoney;
                entity.AlreadyNum = thisTotalNum;
                if (IsNoGet)
                    entity.Status = 1;
                if (IsAllGet)
                    entity.Status = 3;
                if (!IsNoGet && !IsAllGet)
                    entity.Status = 2;
                UpdateEntity(entity);
                scope.SaveChanges();
            }
            return result;
        }


        public ProductItem EntityChange(OutOrderDetail m)
        {
            var ItemEntity = new ProductItem();
            ItemEntity.Id = m.Id;
            ItemEntity.Code = m.Product.Code;
            ItemEntity.SN = m.Product.SN;
            ItemEntity.Specifications = m.Product.Specifications;
            ItemEntity.ProductId = m.Product.Id;
            ItemEntity.Product = m.Product;
            ItemEntity.Name = m.Product.ShortName;
            ItemEntity.Model = m.Product.Model;
            ItemEntity.Num = m.Num;
            ItemEntity.Unit = m.Product.ProductUnit.Name;
            ItemEntity.Price = m.Price;
            ItemEntity.Money = m.Num * m.Price;
            ItemEntity.ChangeNum = m.AlreadyNum;
            ItemEntity.ChangeMoney = m.AlreadyMoney;
            ItemEntity.Coefficient = 1;
            return ItemEntity;
        }
    }
}
